clear
clear matrix
clear mata
set more off, perm
set matsize 11000
set maxvar 30000
set more off, perm
cd /Users/zachbrown/Projects/PriceTransparency/Data/
global output /Users/zachbrown/Projects/PriceTransparency/Output


// open visit dataset
use clean/radiology_visit_3.dta, clear


// Make treatment indicator
gen post = (ym>=ym(2007,3))
gen web_post = (visit_on_web==1 & post==1)
label var web_post "$\text{OnWeb}\textsubscript{m}\times\text{Post}\textsubscript{t}$"


// Set X controls
global ctr_vars1 = "has_copay has_coin##deduct_type age male i.charlindex zip_mean_income zip_pct_ba i.rural_class i.ym i.std_product_type i.payer_id"
global ctr_vars2 = "age male i.charlindex zip_mean_income zip_pct_ba i.rural_class i.ym i.std_product_type i.payer_id"




//////////////////////////////
// Panel 1
//////////////////////////////


areg ln_amt_member_oop web_post $ctr_vars1 if proc_emerg==1, absorb(proc_id_radio) vce(cl ym) 
summ amt_member_oop if e(sample), meanonly
*estadd scalar ymeanexp = r(mean), replace
eststo a6

areg ln_amt_member_oop web_post $ctr_vars1 if proc_emerg==0, absorb(proc_id_radio) vce(cl ym) 
summ amt_member_oop if e(sample), meanonly
*estadd scalar ymeanexp = r(mean), replace
eststo a7


// By Rural
global ctr_vars_r = "has_copay has_coin##deduct_type age male zip_mean_income zip_pct_ba"
gen rural = (rural_class==10)

areg ln_amt_member_oop web_post $ctr_vars1 if rural==1, absorb(proc_id_radio) vce(cl ym) 
summ amt_member_oop if e(sample), meanonly
*estadd scalar ymeanexp = r(mean), replace
eststo a8

areg ln_amt_member_oop web_post $ctr_vars1 if rural==0, absorb(proc_id_radio) vce(cl ym) 
summ amt_member_oop if e(sample), meanonly
*estadd scalar ymeanexp = r(mean), replace
eststo a9


esttab a6 a7 a8 a9 using $output/stratify_regs_c.tex, frag cells(b(fmt(3) star) se(fmt(3) par)) ///
	keep(web_post)  stats() ///
	starlevels(`"\sym{*}"' 0.1 `"\sym{**}"' 0.05 `"\sym{***}"' 0.01, label(" \(p<@\)")) ///
	varwidth(20) modelwidth(12) delimiter(&) end(\\) ///
 	noobs collabels(none) booktabs gaps nonumber label  nonote replace nomtitles nodepvars 
	
esttab a6 a7 a8 a9 using $output/stratify_regs_e.tex, frag	cells(b(fmt(3) star)) ///
	drop(*)  stats(ar2 N, fmt(3 %12.0fc) labels("Adjusted R2" "Observations") layout("\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}")) ///
	varwidth(20) modelwidth(12) delimiter(&) end(\\) collabels(none) nolines ///
	nonumber label  nonote replace nomtitles nodepvars noisily 


// Wald Test

// By emergency 
preserve
gen strat_group = proc_emerg

egen strat_proc_id_radio = group(proc_id_radio strat_group)
egen ym_strat = group(ym strat_group)
gen web_post_strat1 = 0
replace web_post_strat1 = web_post if strat_group==1
gen web_post_strat2 = 0
replace web_post_strat2 = web_post if strat_group==0
quiet areg ln_amt_member_oop web_post_strat1 web_post_strat2 has_copay#i.strat_group has_coin##deduct_type#i.strat_group c.age#strat_group c.male#strat_group i.charlindex#i.strat_group c.zip_mean_income#strat_group c.zip_pct_ba#i.strat_group i.rural_class#i.strat_group i.ym#i.strat_group i.std_product_type#i.strat_group i.payer_id#i.strat_group, absorb(strat_proc_id_radio) vce(cl ym_strat)
test web_post_strat1= web_post_strat2 
estadd scalar Fstat = r(F), replace
estadd scalar pval = r(p), replace
local rF = r(F)
local stars = ""
local rp = r(p)
if `rp'<0.1 local stars = "\sym{*}"
if `rp'<0.05 local  stars = "\sym{**}"
if `rp'<0.01 local  stars = "\sym{***}"
estadd local Fstat_lcl=string(`rF',"%04.3f")+"`stars'"
eststo t1
restore

// By Rural 
preserve
gen strat_group = 1 if rural==1
replace strat_group = 0 if rural==0
drop if strat_group==.

egen strat_proc_id_radio = group(proc_id_radio strat_group)
egen ym_strat = group(ym strat_group)
gen web_post_strat1 = 0
replace web_post_strat1 = web_post if strat_group==1
gen web_post_strat2 = 0
replace web_post_strat2 = web_post if strat_group==0
quiet areg ln_amt_member_oop web_post_strat1 web_post_strat2 has_copay#i.strat_group has_coin##deduct_type#i.strat_group c.age#strat_group c.male#strat_group i.charlindex#i.strat_group c.zip_mean_income#strat_group c.zip_pct_ba#i.strat_group i.rural_class#i.strat_group i.ym#i.strat_group i.std_product_type#i.strat_group i.payer_id#i.strat_group, absorb(strat_proc_id_radio) vce(cl ym_strat)  
test web_post_strat1= web_post_strat2 
estadd scalar Fstat = r(F), replace
estadd scalar pval = r(p), replace
local rF = r(F)
local stars = ""
local rp = r(p)
if `rp'<0.1 local stars = "\sym{*}"
if `rp'<0.05 local  stars = "\sym{**}"
if `rp'<0.01 local  stars = "\sym{***}"
estadd local Fstat_lcl=string(`rF',"%04.3f")+"`stars'"
eststo t2
restore


esttab t1 t2 using $output/stratify_regs_d.tex, frag	cells(b(fmt(3) star)) ///
	drop(*)  stats(Fstat_lcl, fmt(3) labels("\textit{F} statistic of diff." ) layout("\multicolumn{2}{c}{@}")) ///
	varwidth(20) modelwidth(12) delimiter(&) end(\\) collabels(none) nolines ///
	nonumber label  nonote replace nomtitles nodepvars noisily 



//////////////////////////////
// Panel 2
//////////////////////////////

// By age
gen old = (age >35)
replace old = . if age<=18

areg ln_amt_member_oop web_post $ctr_vars1 if old==0, absorb(proc_id_radio) vce(cl ym) 
summ amt_member_oop if e(sample), meanonly
*estadd scalar ymeanexp = r(mean), replace
eststo a12

areg ln_amt_member_oop web_post $ctr_vars1 if old==1, absorb(proc_id_radio) vce(cl ym) 
summ amt_member_oop if e(sample), meanonly
*estadd scalar ymeanexp = r(mean), replace
eststo a13



// By income
egen mean_inc = mean(zip_mean_income)
gen high_inc = (zip_mean_income>mean_inc)
gen low_inc = (zip_mean_income<=mean_inc)

areg ln_amt_member_oop web_post $ctr_vars1 if low_inc==1, absorb(proc_id_radio) vce(cl ym) 
summ amt_member_oop if e(sample), meanonly
*estadd scalar ymeanexp = r(mean), replace
eststo a14

areg ln_amt_member_oop web_post $ctr_vars1 if high_inc==1, absorb(proc_id_radio) vce(cl ym) 
summ amt_member_oop if e(sample), meanonly
*estadd scalar ymeanexp = r(mean), replace
eststo a15



esttab a12 a13 a14 a15  using $output/stratify_regs_f.tex, frag cells(b(fmt(3) star) se(fmt(3) par)) ///
	keep(web_post)  stats() ///
	starlevels(`"\sym{*}"' 0.1 `"\sym{**}"' 0.05 `"\sym{***}"' 0.01, label(" \(p<@\)")) ///
	varwidth(20) modelwidth(12) delimiter(&) end(\\) ///
 	noobs collabels(none) booktabs gaps nonumber label  nonote replace nomtitles nodepvars 
	
esttab a12 a13 a14 a15  using $output/stratify_regs_h.tex, frag	cells(b(fmt(3) star)) ///
	drop(*)  stats(ar2 N, fmt(3 %12.0fc) labels("Adjusted R2" "Observations") layout("\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}")) ///
	varwidth(20) modelwidth(12) delimiter(&) end(\\) collabels(none) nolines ///
	nonumber label  nonote replace nomtitles nodepvars noisily 

// Wald Test

// By age
preserve
gen strat_group = 1 if old==1
replace strat_group = 0 if old==0
drop if strat_group==.

egen strat_proc_id_radio = group(proc_id_radio strat_group)
egen ym_strat = group(ym strat_group)
gen web_post_strat1 = 0
replace web_post_strat1 = web_post if strat_group==1
gen web_post_strat2 = 0
replace web_post_strat2 = web_post if strat_group==0
quiet areg ln_amt_member_oop web_post_strat1 web_post_strat2 has_copay#i.strat_group has_coin##deduct_type#i.strat_group c.age#strat_group c.male#strat_group i.charlindex#i.strat_group c.zip_mean_income#strat_group c.zip_pct_ba#i.strat_group i.rural_class#i.strat_group i.ym#i.strat_group i.std_product_type#i.strat_group i.payer_id#i.strat_group, absorb(strat_proc_id_radio) vce(cl ym_strat)
test web_post_strat1= web_post_strat2 
estadd scalar Fstat = r(F), replace
estadd scalar pval = r(p), replace
local rF = r(F)
local stars = ""
local rp = r(p)
if `rp'<0.1 local stars = "\sym{*}"
if `rp'<0.05 local  stars = "\sym{**}"
if `rp'<0.01 local  stars = "\sym{***}"
estadd local Fstat_lcl=string(`rF',"%04.3f")+"`stars'"
eststo t4
restore


// By income
preserve
gen strat_group = 1 if low_inc==1
replace strat_group = 0 if high_inc==1
drop if strat_group==.

egen strat_proc_id_radio = group(proc_id_radio strat_group)
egen ym_strat = group(ym strat_group)
gen web_post_strat1 = 0
replace web_post_strat1 = web_post if strat_group==1
gen web_post_strat2 = 0
replace web_post_strat2 = web_post if strat_group==0
quiet areg ln_amt_member_oop web_post_strat1 web_post_strat2 has_copay#i.strat_group has_coin##deduct_type#i.strat_group c.age#strat_group c.male#strat_group i.charlindex#i.strat_group c.zip_mean_income#strat_group c.zip_pct_ba#i.strat_group i.ym#i.strat_group i.std_product_type#i.strat_group i.payer_id#i.strat_group, absorb(strat_proc_id_radio) vce(cl ym_strat)  
test web_post_strat1= web_post_strat2 
estadd scalar Fstat = r(F), replace
estadd scalar pval = r(p), replace
local rF = r(F)
local stars = ""
local rp = r(p)
if `rp'<0.1 local stars = "\sym{*}"
if `rp'<0.05 local  stars = "\sym{**}"
if `rp'<0.01 local  stars = "\sym{***}"
estadd local Fstat_lcl=string(`rF',"%04.3f")+"`stars'"
eststo t6
restore

esttab t4 t6 using $output/stratify_regs_g.tex, frag	cells(b(fmt(3) star)) ///
	drop(*)  stats(Fstat_lcl, fmt(3 3) labels("\textit{F} statistic of diff.") layout("\multicolumn{2}{c}{@}")) ///
	varwidth(20) modelwidth(12) delimiter(&) end(\\) collabels(none) nolines ///
	nonumber label  nonote replace nomtitles nodepvars noisily 

